<?php
/**
 * Created by PhpStorm.
 * User: varch
 * Date: 2019/12/24
 * Time: 23:42
 */

//namespace TxCloudAPI;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Billing\V20180709\Models\DescribeAccountBalanceRequest;
use TencentCloud\Billing\V20180709\BillingClient;

class ObserverTxCloudBalance extends ObserverTxCloud
{
    private function getBalance():string
    {
        try {
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("billing.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new BillingClient($this->cred, "", $clientProfile);

            $req = new DescribeAccountBalanceRequest();

            $params = '{}';
            $req->fromJsonString($params);


            $resp = $client->DescribeAccountBalance($req);

            return $resp->toJsonString();
        } catch (TencentCloudSDKException $e) {
            Yii::error("Get TxCloud Balance Failed. Reason = ".$e);
            return $e->getMessage();
        }

    }

    public function observe(string $function = "PING") : void
    {
        switch ($function) {
            case "PING": echo "PONG"; break;
            case "BALANCE":
                $balanceString = $this->getBalance();
                echo $balanceString;
                break;
        }

    }
}